/**
 * admui-basic v2.1.0 (http://www.admui.com/)
 * Copyright 2015-2019 Admui Team
 * Licensed under the Admui License 1.1 (http://www.admui.com/about/license)
 */
(function(document, window, $) {
  'use strict';

  // 缩放示例
  $('#exampleZoomGallery').magnificPopup(
    $.concatCpt('magnificPopup', {
      delegate: 'a',
      type: 'image',
      closeOnContentClick: false,
      closeBtnInside: false,
      mainClass: 'mfp-with-zoom mfp-img-mobile',
      image: {
        verticalFit: true,
        titleSrc: function(item) {
          return (
            item.el.attr('title') +
            ' &middot;  <a class="image-source-link" href="' +
            item.el.attr('data-source') +
            '" target="_blank">查看图片</a>'
          );
        }
      },
      gallery: {
        enabled: true
      },
      zoom: {
        enabled: true,
        duration: 300, // CSS 动画也要设置同样的延迟时间
        opener: function(element) {
          return element.find('img');
        }
      }
    })
  );

  // 相册示例
  $('#exampleGallery').magnificPopup(
    $.concatCpt('magnificPopup', {
      delegate: 'a',
      type: 'image',
      tLoading: '正在加载图片 #%curr%...',
      mainClass: 'mfp-img-mobile',
      gallery: {
        enabled: true,
        navigateByImgClick: true,
        preload: [0, 1]
      },
      image: {
        tError: '无法加载图片 <a href="%url%"> #%curr%</a>。',
        titleSrc: function(item) {
          return item.el.attr('title') + '<small>by admui.com</small>';
        }
      }
    })
  );

  // CSS 动画示例
  $('.popup-with-css-anim').magnificPopup(
    $.concatCpt('magnificPopup', {
      type: 'image',
      removalDelay: 500,
      preloader: true,
      callbacks: {
        beforeOpen: function() {
          this.st.image.markup = this.st.image.markup.replace(
            'mfp-figure',
            'mfp-figure mfp-with-anim'
          );
          this.st.mainClass = this.st.el.attr('data-effect');
        }
      },
      closeOnContentClick: true,
      midClick: true
    })
  );

  // 视频 | 地图示例
  $('.popup-youku, .popup-tengxun, .popup-baidumaps').magnificPopup(
    $.concatCpt('magnificPopup', {
      disableOn: 700,
      type: 'iframe',
      mainClass: 'mfp-fade',
      removalDelay: 160,
      preloader: false,

      fixedContentPos: false
    })
  );

  // 表单示例
  $('#examplePopupForm').magnificPopup(
    $.concatCpt('magnificPopup', {
      type: 'inline',
      preloader: false,
      focus: '#inputName',
      callbacks: {
        beforeOpen: function() {
          /* 当表单第一个元素自动获得焦点时，一些手机浏览器或产生缩放效果
            这样非常影响外观，所以我们在分辨率较小时禁用了自动获得焦点 */
          if ($(window).width() <= 768) {
            this.st.focus = false;
          } else {
            this.st.focus = '#inputName';
          }
        }
      }
    })
  );

  // Ajax 示例
  $('#examplePopupAjaxAlignTop').magnificPopup(
    $.concatCpt('magnificPopup', {
      type: 'ajax',
      alignTop: true,
      overflowY: 'scroll' // 设置滚动条避免文本溢出
    })
  );

  // Ajax 示例
  $('#examplePopupAjax').magnificPopup(
    $.concatCpt('magnificPopup', {
      type: 'ajax'
    })
  );

  // 模态框示例
  $('.popup-modal').magnificPopup({
    type: 'inline',
    preloader: false,
    modal: true
  });

  // 关闭模态框
  $(document).on('click', '.popup-modal-dismiss', function(e) {
    e.preventDefault();
    $.magnificPopup.close();
  });

  // 错误处理示例
  $('#exampleBrokenImage, #exampleBrokenAjax').magnificPopup($.concatCpt('magnificPopup'));
})(document, window, jQuery);
